//
//	Simple Hex-View application 
//	Copyright J Brown
//	Freeware, use at your own risk etc!
//
#include <windows.h>
#include "HexView.h"
#include "resource.h"

TCHAR szAppName[] = "HexView";
HINSTANCE hInstance;
HWND hwndMain;
HWND hwndHexView;

TCHAR szFileName[MAX_PATH];
TCHAR szFileTitle[MAX_PATH];

static OPENFILENAME ofn;
static char szFilter[] = "All Files (*.*)\0*.*\0\0";

void OpenFileInitialize(HWND hwnd)
{
	ofn.lStructSize			= sizeof(OPENFILENAME);
	ofn.hwndOwner			= hwnd;
	ofn.hInstance			= hInstance;
	ofn.lpstrFilter			= szFilter;
	ofn.lpstrCustomFilter	= NULL;
	ofn.nMaxCustFilter		= 0;//sizeof(szFilter);
	ofn.nFilterIndex		= 1;
	ofn.lpstrFile			= NULL;			//set in Open & Save functions
	ofn.nMaxFile			= _MAX_PATH;
	ofn.lpstrFileTitle		= NULL;			//set in Open & Save functions
	ofn.nMaxFileTitle		= _MAX_FNAME + _MAX_EXT;
	ofn.lpstrInitialDir		= NULL;
	ofn.lpstrTitle			= NULL;
	ofn.Flags				= 0;			//set in Open & Save functions
	ofn.nFileOffset			= 0;
	ofn.nFileExtension		= 0;
	ofn.lpstrDefExt			= NULL;
	ofn.lCustData			= 0L;
	ofn.lpfnHook			= NULL;
	ofn.lpTemplateName		= NULL;
}

BOOL DoFileOpen(HWND hwnd, PSTR pstrFileName, PSTR pstrTitleName)
{
	OpenFileInitialize(hwnd);

	ofn.hwndOwner		= hwnd;
	ofn.lpstrFile		= pstrFileName;
	ofn.lpstrFileTitle	= pstrTitleName;
	ofn.Flags			= OFN_EXPLORER | OFN_ENABLESIZING |
							OFN_ALLOWMULTISELECT | 
							OFN_FILEMUSTEXIST|OFN_HIDEREADONLY;

	if(GetOpenFileName (&ofn)) 
	{
		//make sure that we don't re-open the current file
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static int width, height;
	char ach[MAX_PATH+sizeof(szAppName)+4];

	switch(msg)
	{
	case WM_CREATE:
		hwndHexView = CreateHexView(hwnd);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDM_FILE_OPEN:

			if(DoFileOpen(hwnd, szFileName, szFileTitle))
			{
				wsprintf(ach, "%s - %s", szFileName, szAppName);
				SetWindowText(hwnd, ach);
				HexView_OpenFile(hwndHexView, szFileName);

			}
			return 0;
		}
		return 0;

	case WM_CLOSE:
		DestroyWindow(hwnd);
		return 0;

	case WM_SIZE:
		width  = (short)LOWORD(lParam);
		height = (short)HIWORD(lParam);

		MoveWindow(hwndHexView, 0, 0, width, height, TRUE);
		return 0;

	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int iShowCmd)
{
	HWND		hwnd;
	MSG			msg;
	WNDCLASSEX	wndclass;

	hInstance = hInst;			

	//Window class for the main application parent window
	wndclass.cbSize			= sizeof(wndclass);
	wndclass.style			= 0;
	wndclass.lpfnWndProc	= WndProc;
	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= LoadIcon(0, MAKEINTRESOURCE(IDI_APPLICATION));
	wndclass.hCursor		= LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground	= (HBRUSH)0;
	wndclass.lpszMenuName	= MAKEINTRESOURCE(IDR_MENU1);
	wndclass.lpszClassName	= szAppName;
	wndclass.hIconSm		= LoadIcon (NULL, IDI_APPLICATION);

	RegisterClassEx(&wndclass);

	InitHexView();

	hwnd = CreateWindowEx(0,
				szAppName,		// window class name
				szAppName,		// window caption
				WS_OVERLAPPEDWINDOW,//|WS_CLIPCHILDREN,
				CW_USEDEFAULT,			// initial x position
				CW_USEDEFAULT,			// initial y position
				560,					// initial x size
				320,			// initial y size
				NULL,					// parent window handle
				NULL,					// use window class menu
				hInstance,				// program instance handle
				NULL);					// creation parameters

	hwndMain = hwnd;

	ShowWindow(hwnd, iShowCmd);

	while(GetMessage(&msg, NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}